package com.jiesen.resource.modules.uploadFile.service.imp;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.jiesen.resource.basicComponents.baseDao.BaseServiceImpl;
import com.jiesen.resource.modules.uploadFile.dao.FileDao;
import com.jiesen.resource.modules.uploadFile.pojo.FIleAttributesFrom;
import com.jiesen.resource.modules.uploadFile.model.FileAttributes;
import com.jiesen.resource.modules.uploadFile.service.FileService;
import com.jiesen.resource.utils.QiniuUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

@Service
public class FileServiceImp extends BaseServiceImpl<FileDao,FileAttributes> implements FileService {

    @Autowired
    private FileDao fileDao;

    @Override
    @Transactional
    public FIleAttributesFrom uploadToQiniu(FileAttributes fileAttributes) {
        //项目存放地址
        String path = QiniuUtil.getUrlPrefix()+"/"+fileAttributes.getServerName();
        fileAttributes.setUrl(path);
        //上传至七牛云
        fileAttributes.setServerType(1);

        //文件属性添加数据库
        int insert = fileDao.insert(fileAttributes);
        if(!(insert>0)){
            throw new RuntimeException("添加数据库失败");
        }

        //上传文件
        QiniuUtil.fileUploadToQiniu(fileAttributes.getMultipartFile(),fileAttributes.getServerName());

        return new FIleAttributesFrom(fileAttributes);
    }

    @Override
    public List<FileAttributes> findAll() {
        return fileDao.selectList(null);
    }

    @Override
    public List<FileAttributes> findByType(Integer id) {
        QueryWrapper<FileAttributes> fileAttributesQueryWrapper = new QueryWrapper<>();
        fileAttributesQueryWrapper.eq("server_type",id);
        return fileDao.selectList(fileAttributesQueryWrapper);
    }

    @Override
    public FileAttributes findById(Integer id) {
        return fileDao.selectById(id);
    }

    @Override
    public int InsertFileAttributes(FileAttributes fileAttributes) {
        if(fileAttributes.getName()==null) {
            fileAttributes.setName(fileAttributes.getOldName());
        }
        if(fileAttributes.getOldName()==null){
            fileAttributes.setOldName(fileAttributes.getName());
        }
        if(fileAttributes.getUrl()==null){
            fileAttributes.setUrl(QiniuUtil.getUrlPrefix()+"/"+fileAttributes.getServerName());
        }
        if(fileAttributes.getServerType()==null){
            fileAttributes.setServerType(1);
        }
        String oldName = fileAttributes.getOldName();
        fileAttributes.setSuffixName(oldName.substring(oldName.lastIndexOf(".")));

        fileAttributes.setUpdateTime(new Date());

        return fileDao.insert(fileAttributes);
    }

    @Override
    public List<FileAttributes> findByFolderId(Integer id) {
        QueryWrapper<FileAttributes> fileAttributesQueryWrapper = new QueryWrapper<>();
        fileAttributesQueryWrapper.eq("folder_id",id);
        return fileDao.selectList(fileAttributesQueryWrapper);
    }

    @Override
    @Transactional
    public int deleteById(Integer id) {
        FileAttributes fileAttributes = fileDao.selectById(id);
        int i = fileDao.deleteById(id);
        QiniuUtil.fileDeleteToQiniu(fileAttributes.getServerName());
        return i;
    }

    @Override
    public boolean updateById(FileAttributes fileAttributes) {
        FileAttributes fileAttributes1 = fileDao.selectById(fileAttributes.getId());
        fileAttributes1.setName(fileAttributes1.getName());
        fileAttributes1.setUpdateTime(new Date());
        fileAttributes1.setUpdateOperator(fileAttributes.getUpdateOperator());
        fileAttributes1.setUpdateOperatorUrl(fileAttributes.getUpdateOperatorUrl());
        return fileDao.updateById(fileAttributes1)>0;
    }

}
